package com.everflourish.yeah100.ui.dialog;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.everflourish.yeah100.R;
import com.everflourish.yeah100.utils.MyToast;
import com.everflourish.yeah100.utils.StringUtil;

/**
 * 作者：Created by liqisi on 2016/10/26.
 * 时间：2016/10/26
 */
public class KeyboardWindow extends PopupWindow implements View.OnClickListener {

    private View popupView;
    private EditText inputContentTv;
    private TextView titleTv;
    private TextView previewBt, nextBt;
    private LinearLayout deletBt;
    private OnNextListener onNextListener;
    private OnPreviewListener onPreviewListener;
    private Context mContext;
    private boolean isAllClean;

    public KeyboardWindow(Context context, final Activity activity, String title) {
        super(context);
        this.mContext = context;
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        popupView = inflater.inflate(R.layout.dialog_keyboard, null);
        TextView key0 = (TextView) popupView.findViewById(R.id.keyboard_0);
        key0.setTag("0");
        key0.setOnClickListener(this);
        TextView key1 = (TextView) popupView.findViewById(R.id.keyboard_1);
        key1.setTag("1");
        key1.setOnClickListener(this);
        TextView key2 = (TextView) popupView.findViewById(R.id.keyboard_2);
        key2.setTag("2");
        key2.setOnClickListener(this);
        TextView key3 = (TextView) popupView.findViewById(R.id.keyboard_3);
        key3.setTag("3");
        key3.setOnClickListener(this);
        TextView key4 = (TextView) popupView.findViewById(R.id.keyboard_4);
        key4.setTag("4");
        key4.setOnClickListener(this);
        TextView key5 = (TextView) popupView.findViewById(R.id.keyboard_5);
        key5.setTag("5");
        key5.setOnClickListener(this);
        TextView key6 = (TextView) popupView.findViewById(R.id.keyboard_6);
        key6.setTag("6");
        key6.setOnClickListener(this);
        TextView key7 = (TextView) popupView.findViewById(R.id.keyboard_7);
        key7.setTag("7");
        key7.setOnClickListener(this);
        TextView key8 = (TextView) popupView.findViewById(R.id.keyboard_8);
        key8.setTag("8");
        key8.setOnClickListener(this);
        TextView key9 = (TextView) popupView.findViewById(R.id.keyboard_9);
        key9.setTag("9");
        key9.setOnClickListener(this);
        TextView keyDian = (TextView) popupView.findViewById(R.id.keyboard_dian);
        keyDian.setTag(".");
        keyDian.setOnClickListener(this);
        TextView clearBt = (TextView) popupView.findViewById(R.id.keyboard_clear);
        clearBt.setOnClickListener(this);
        isAllClean = true;

        inputContentTv = (EditText) popupView.findViewById(R.id.dialog_score_et);
        previewBt = (TextView) popupView.findViewById(R.id.preview_bt);
        previewBt.setOnClickListener(this);
        nextBt = (TextView) popupView.findViewById(R.id.next_bt);
        nextBt.setOnClickListener(this);
        deletBt = (LinearLayout) popupView.findViewById(R.id.delete_bt);
        deletBt.setOnClickListener(this);

        titleTv = (TextView) popupView.findViewById(R.id.title_tv);
        titleTv.setText(title);

        this.setContentView(popupView);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
//        WindowManager.LayoutParams att = activity.getWindow().getAttributes();
//        att.alpha = 0.4f;
//        activity.getWindow().setAttributes(att);
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
//        mPopupWindow.setBackgroundDrawable(mContext.getResources().getDrawable(
//                R.drawable.popup_bg));
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
//        mPopupWindow.showAsDropDown(v, 0, 2);
        this.setAnimationStyle(R.style.PopupAnimation);
        ColorDrawable dw = new ColorDrawable(0x00000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        this.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                // holderView.nOperationTv.setText("操作");
                WindowManager.LayoutParams att = activity.getWindow().getAttributes();
                att.alpha = 1f;
                activity.getWindow().setAttributes(att);
            }
        });

    }

    public void setTitle(String title) {
        titleTv.setText(title);
    }

    public void setInputContent(String content) {
        inputContentTv.setText(content);
    }

    public Float getScore() {
        String scoreStr = inputContentTv.getText().toString();
        return StringUtil.parseFloat(scoreStr, 0f);
    }

    public void setPreviewListener(OnPreviewListener listener) {
        onPreviewListener = listener;
    }

    public void setPreviewText(String text) {
        previewBt.setText(text);
    }

    public void setNextListener(OnNextListener listener) {
        onNextListener = listener;
    }

    public void setNextText(String text) {
        nextBt.setText(text);
    }

    @Override
    public void onClick(View v) {
        if(isAllClean){
            inputContentTv.setBackground(null);
            inputContentTv.setText("");
            isAllClean = false;
        }
        String number = (String) v.getTag();
        String scoreStr = inputContentTv.getText().toString();
        if (number != null) {
            if (scoreStr.equals("0.0")) {
                inputContentTv.setText(number);
                return;
            }
            if (scoreStr.contains(".") && number.equals(".")) {
                return;
            }
            int indexOf = scoreStr.indexOf(".");
            if (indexOf != -1) {
                String behindNumber = scoreStr.substring(indexOf,
                        scoreStr.length());
                if (behindNumber.length() > 1) {
                    MyToast.showLong(mContext, "小数点保留一位小数");
                    return;
                }
                String beginNumber = scoreStr.substring(0,
                        indexOf);
                if (beginNumber.length() > 6) {
                    MyToast.showLong(mContext, "分值超出限制");
                    return;
                }
            } else {
                if (scoreStr.length() >= 6) {
                    MyToast.showLong(mContext, "分值超出限制");
                    return;
                }
            }
            if (scoreStr.equals("0") && number.equals("0")) {
                return;
            }
            inputContentTv.setText(scoreStr + number);
            return;
        }
        switch (v.getId()) {
            case R.id.next_bt:
                if (onNextListener != null) {
                    onNextListener.next();
                }
                break;
            case R.id.preview_bt:
                if (onPreviewListener != null) {
                    onPreviewListener.preview();
                }
                break;
            case R.id.delete_bt:
                if (scoreStr.length() > 0) {
                    inputContentTv.setText(scoreStr.substring(0, scoreStr.length() - 1));
                }
                break;
            case R.id.keyboard_clear:
                inputContentTv.setText("");
                break;
        }
    }

    public interface OnNextListener {
        void next();
    }

    public interface OnPreviewListener {
        void preview();
    }
}
